﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using BackupLibrary;
using MasterLibrary.Windows.Forms.Aero;

namespace Prometheus
{
    public partial class wzSettingsData : WizardPage
    {
        public BackupContainer backupContainer { get; set; }

        public event EventHandler<wzSettingsDataChangedEventArgs> ContainerChanged;
        private void OnContainerChanged(wzSettingsDataChangedEventArgs e)
        {
            EventHandler<wzSettingsDataChangedEventArgs> ev = ContainerChanged;
            if (ev != null) ev(this, e);
        }

        public wzSettingsData(BackupContainer c)
        {
            InitializeComponent();

            this.backupContainer = c;
            this.ReloadBackupCollection();
        }

        public void ReloadBackupCollection()
        {
            this.SuspendLayout();
            this.listView.SuspendLayout();

            this.listView.Clear();

            this.listView.Columns.Add("Quellpfad", 430, HorizontalAlignment.Left);
            this.listView.Columns.Add("Aktiv", 50);

            foreach (BackupItem val in this.backupContainer.BackupItems)
            {
                if (val.Type == ItemType.File)
                {
                    if (File.Exists(val.SourcePath))
                    {
                        this.listView.Items.Add(new ListViewItem(new string[] { val.SourcePath, (val.Active) ? "Ja" : "Nein" }, (val.Active) ? 3 : 4));
                    }
                    else
                    {
                        this.listView.Items.Add(new ListViewItem(new string[] { val.SourcePath, (val.Active) ? "Ja" : "Nein" }, 5));
                    }
                }
                else
                {
                    if (Directory.Exists(val.SourcePath))
                    {
                        this.listView.Items.Add(new ListViewItem(new string[] { val.SourcePath, (val.Active) ? "Ja" : "Nein" }, (val.Active) ? 0 : 1));
                    }
                    else
                    {
                        this.listView.Items.Add(new ListViewItem(new string[] { val.SourcePath, (val.Active) ? "Ja" : "Nein" }, 2));
                    }
                }
            }

            this.listView.ResumeLayout();
            this.ResumeLayout();          
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            BackupItem backupItem = new BackupItem();
            backupItem.Type = ItemType.Folder;

            using (BackupItemForm newItem = new BackupItemForm(backupItem, false))
            {
                DialogResult result = newItem.ShowDialog(Host);

                if (result == DialogResult.OK)
                {
                    this.backupContainer.BackupItems.Add(newItem.backupItem);
                    this.ReloadBackupCollection();

                    this.OnContainerChanged(new wzSettingsDataChangedEventArgs(this.backupContainer));
                }
            }
        }

        private void btnAddFile_Click(object sender, EventArgs e)
        {
            BackupItem backupItem = new BackupItem();
            backupItem.Type = ItemType.File;

            using (BackupItemForm newItem = new BackupItemForm(backupItem, false))
            {
                DialogResult result = newItem.ShowDialog(Host);

                if (result == DialogResult.OK)
                {
                    this.backupContainer.BackupItems.Add(newItem.backupItem);
                    this.ReloadBackupCollection();

                    this.OnContainerChanged(new wzSettingsDataChangedEventArgs(this.backupContainer));
                }
            }
        }

        private void löschenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.btnDelete_Click(sender, e);
        }
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.listView.SelectedItems.Count == 1)
            {
                this.backupContainer.BackupItems.RemoveAt(this.listView.SelectedItems[0].Index);

                this.ReloadBackupCollection();
                this.OnContainerChanged(new wzSettingsDataChangedEventArgs(this.backupContainer));
            }
        }

        private void bearbeitenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listView_DoubleClick(sender, e);
        }
        private void listView_DoubleClick(object sender, EventArgs e)
        {
            if (this.listView.SelectedItems.Count == 1)
            {
                using (BackupItemForm newItem = new BackupItemForm(this.backupContainer.BackupItems[this.listView.SelectedItems[0].Index], true))
                {
                    DialogResult result = newItem.ShowDialog(Host);

                    if (result == DialogResult.OK)
                    {
                        this.backupContainer.BackupItems[this.listView.SelectedItems[0].Index].AddSourceFolder = newItem.backupItem.AddSourceFolder;
                        this.backupContainer.BackupItems[this.listView.SelectedItems[0].Index].ExcludeContext = newItem.backupItem.ExcludeContext;
                        this.backupContainer.BackupItems[this.listView.SelectedItems[0].Index].FileType = newItem.backupItem.FileType;
                        this.backupContainer.BackupItems[this.listView.SelectedItems[0].Index].IncludeContext = newItem.backupItem.IncludeContext;
                        this.backupContainer.BackupItems[this.listView.SelectedItems[0].Index].IncludeSubfolder = newItem.backupItem.IncludeSubfolder;
                        this.backupContainer.BackupItems[this.listView.SelectedItems[0].Index].SourcePath = newItem.backupItem.SourcePath;

                        this.ReloadBackupCollection();
                        this.OnContainerChanged(new wzSettingsDataChangedEventArgs(this.backupContainer));
                    }
                }
            }
        }

        private void contextMenu_Opened(object sender, EventArgs e)
        {
            if (this.listView.SelectedItems.Count == 1)
            {
                this.contextMenu.Enabled = true;
                this.deaktivierenToolStripMenuItem.Enabled = this.backupContainer.BackupItems[this.listView.SelectedItems[0].Index].Active;
                this.aktivierenToolStripMenuItem.Enabled = !this.backupContainer.BackupItems[this.listView.SelectedItems[0].Index].Active;
            }
            else
            {
                this.contextMenu.Enabled = false;
            }
        }

        private void deaktivierenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.listView.SelectedItems.Count == 1)
            {
                this.backupContainer.BackupItems[this.listView.SelectedItems[0].Index].Active = false;

                this.ReloadBackupCollection();
                this.OnContainerChanged(new wzSettingsDataChangedEventArgs(this.backupContainer));
            }
        }

        private void aktivierenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.listView.SelectedItems.Count == 1)
            {
                this.backupContainer.BackupItems[this.listView.SelectedItems[0].Index].Active = true;

                this.ReloadBackupCollection();
                this.OnContainerChanged(new wzSettingsDataChangedEventArgs(this.backupContainer));
            }
        }
    }

    #region EventArgs
    public class wzSettingsDataChangedEventArgs : EventArgs
    {
        public BackupContainer Container { get; set; }

        public wzSettingsDataChangedEventArgs(BackupContainer Container)
        {
            this.Container = Container;
        }
    }
    #endregion
}
